<script>

function filter_graph(pins){


        let toKeep = [];

        let toKeepPinned = [];

        let toKeepMap = [];


        // What are the nodes that we want to filter?

        if (pins.length > 0) {

            // Reiterate for each pinned node

            for (var i = 0; i < pins.length; i ++) {

                // Now we reiterate through every node in the graph

                sigma.instances(0).graph.nodes().filter(function(n) {

                    // Is the current node of the graph one of the ones that was clicked before?

                    if (pins[i] == n.originalLabel) {

                        // The ID of the current node is...
                        var nodeId = n.id;

                        // Its neighbors are...
                        var nnodes = sigma.instances(0).graph.neighbors(nodeId);

                        var nnodesArray = [];

                        // We create an array from neighbors that looks like toKeep one

                        for (var key in nnodes) {
                            nnodesArray.push(nnodes[key]);
                        }

                        
                        if (i > 0) {
                            // If that's not the first iteration, we only keep intersection of both
                            toKeep = _.intersection(toKeep, nnodesArray);
                        }
                        else {
                            // If that is the first interaction, we create toKeep array from the neighbors only
                            toKeep = nnodesArray;
                        }

                        // We also add the actual node to another array
                        toKeepPinned.push(n);


                    }

                });

            }

            // Make array of nodes to keep unique to avoid duplicates

            toKeep = _.uniq(toKeep);

            // We make a list of the nodes to keep which includes the pinnedNodes and its shared neighbors

            toKeep = _.union(toKeep,toKeepPinned);

            

            console.log(toKeep.length + ' nodes selected out of ' + graphFactory.getStats('TotalNodes') + ' (' + (toKeep.length/graphFactory.getStats('TotalNodes') ).toFixed(2)*100 + '%)')
            
            // Save which nodes are selected on the graph for community detection

            graphFactory.updateSelectedNodes([]);

            for (let irr in toKeep) {
                graphFactory.addToSelectedNodes(toKeep[irr].originalLabel);
            }

            // Now convert this to an array that will be understood by the functions below

            for (let key in toKeep) {
                toKeepMap[toKeep[key].id] = toKeep[key];
            }

            // Let's now color the nodes in the graph accordingly

            sigma.instances(0).graph.nodes().forEach(function(n) {

                // Is the current node iterated in the list to be shown?

                if (toKeepMap[n.id])  {

                    if (pins.indexOf(n.originalLabel) > -1) {

                        n.color = '#0089e0';

                        // Display info on the last node selected
                        if (n.originalLabel == toKeepPinned[toKeepPinned.length-1].label) {
                            console.log('The node "' + n.originalLabel + '" has ' + n.size + ' edges (degree), betweenness centrality: ' + n.bc + ', belongs to ' + n.community + ' community.');
                        }

                    }
                    else {

                        n.color = n.originalColor;

                    }

                

                        n.label = n.originalLabel;
                    
                  

                }
                else  {
                    n.color = userFactory.getGraphOption('deselected_nodes_color');
                    n.label = '';
                }

            });

            // Do the same for the edges

            sigma.instances(0).graph.edges().forEach(function(e) {
                    if (toKeepMap[e.source] && toKeepMap[e.target]) {
                        e.color = e.originalColor;
                    }
                    else {
                        e.color = userFactory.getGraphOption('deselected_nodes_color');
                    }
            });



        }

        // No nodes are selected (or all are deselected) - show the whole graph

        else {

            console.log('deselecting the graph');

            sigma.instances(0).graph.nodes().forEach(function(n) {

                n.color = n.originalColor;
                // if (!n.iscontext) {
                //     n.label = n.originalLabel;
                // }
                n.label = n.originalLabel;


            });

            sigma.instances(0).graph.edges().forEach(function(e) {

                e.color = e.originalColor;

            });

            // Remove all selections
            graphFactory.updateSelectedNodes([]);

            // No communities clicked
            commClicked = [];

        }

        // Since the data has been modified, we need to
        // call the refresh method to make the colors
        // update effective.

        sigma.instances(0).refresh();

        // Let's play the graph MIDI sounds on selection
        playNodesMIDI().selection(pins, toKeep);


}


function resetGraph() {
    socket.emit('graph reset');
}

function deleteNodes() {
    socket.emit('node delete', {pinnedNodes: graphFactory.getPinnedNodes()});
}


// What happens when we delete a node
socket.on('node delete', function(msg){
    
    // Which nodes are we going to delete?
    let nodesToDelete = msg.pinnedNodes;

    graphFactory.deleteNodes(nodesToDelete);
 
    $('.community_node').each(function(){
        $(this).removeAttr('style');
    });

    $('.top_nodes').each(function(){
        $(this).removeAttr('style');
    });


});


// What happens when we add a node ?

socket.on('node add', function(msg){

        let addingNodes = msg.addingNodes;
        
        graphFactory.addDeletedNode(addingNodes);

});



// What happens when the user clicks on a node in the graph (this is caleld from sigma but processed here)
// This is for collaboration only

socket.on('node click', function(msg){
    
        graphFactory.initPinnedNodes(msg.pinnedNodes);   

});


// When we click reset button

socket.on('graph reset', function(msg){
     
     graphFactory.initPinnedNodes();
     graphFactory.updateNotFoundNodes();
     graphFactory.updateSelectedNodes([]);
     graphFactory.clearSelectCommunity();
     analyticsPanel.removeSelections();
     statements().unfilter();
      
 });




</script>